package com.rzl.expense.quartz.task;


import com.rzl.expense.agent.DataAgentService;
import com.rzl.expense.expense.domain.RptPrvRentBasesiteResource;
import com.rzl.expense.expense.service.IRentBillamountService;
import com.rzl.expense.expense.service.IRptPrvRentBasesiteResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 房租账单汇总
 *
 * @author admin
 * @date 2023/08/09
 */
@Slf4j
@Component("RptPrvRentBasesiteResourceTask")
public class RptPrvRentBasesiteResourceTask {
    @Autowired
    private DataAgentService dataAgentService;

    @Autowired
    private IRptPrvRentBasesiteResourceService rptPrvRentBasesiteResourceService;

    public void run() {
        log.info("获取集团用户数据..........");
        // 执行sql

        int size;
        // limit起始位置
        int startIndex = 0;
        // limit大小
        int limit = 20000;
        rptPrvRentBasesiteResourceService.deleteRptPrvRentBasesiteResource();
        do {

            List<RptPrvRentBasesiteResource> list = (List<RptPrvRentBasesiteResource>) dataAgentService.getData(RuleSqlAdapter.getRptPrvRentBasesiteResource(startIndex, limit), RptPrvRentBasesiteResource.class);
            if (!Objects.isNull(list) && list.size() > 0) {
                int insertSize = list.size();
                // limit起始位置
                int insertIndex = 0;
                // limit大小
                do {
                    List<RptPrvRentBasesiteResource> paymentResourceDetails = list.stream().skip(insertIndex).limit(1000).collect(Collectors.toList());
                    try {
                        if (!Objects.isNull(paymentResourceDetails) && paymentResourceDetails.size() > 0) {
                            rptPrvRentBasesiteResourceService.batchRptPrvRentBasesiteResource(paymentResourceDetails);
                            insertSize = paymentResourceDetails.size();
                            insertIndex = insertIndex + 1000;
                        } else {
                            insertSize = 0;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println(paymentResourceDetails);
                    }
                } while (insertSize >= 1000);
            }
            size = list.size();
            startIndex = startIndex + limit;
        } while (size >= limit);

        log.info("数据操作成功");
    }
}
